package com.amos.generate.service;

import com.amos.generate.bean.BeanField;
import com.amos.generate.bean.DataBaseField;
import com.amos.generate.bean.JavaField;
import com.amos.generate.type.TemplateTypeEnum;
import com.amos.generate.util.SpringContext;
import com.amos.generate.util.VelocityUtils;
import com.amos.util.FieldUtils;
import com.amos.util.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Copyright © 2018 嘉源锐信. All rights reserved.
 *
 * @Project: mu-exam
 * @ClassName: AbstractColumnHandler
 * @Package: com.amos.generate.service
 * @author: zhuqb
 * @Description:
 * @date: 2020/3/11 0011 下午 14:45
 * @Version: V1.0
 */
public abstract class AbstractColumnHandler<T extends BeanField> extends AbstractHandler<T> {

    private String path;
    private String rootPackageName;
    private String tableName;
    private boolean isPage;
    private TemplateTypeEnum templateType;

    private List<DataBaseField> list;

    public AbstractColumnHandler(String path, String rootPackageName, String tableName, TemplateTypeEnum templateType,boolean isPage, List<DataBaseField> list) {
        super(path, rootPackageName, tableName, templateType);
        this.path = path;
        this.rootPackageName = rootPackageName;
        this.tableName = tableName;
        this.isPage = isPage;
        this.list = list;
    }

    /**
     * 获取需要过滤的class对象
     * @return
     */
    protected abstract Class<?> getFilterClass();


    @Override
    public T field() {
        return this.getColumns();
    }


    public T getColumns(){
        // 过滤基类的字段
        list = filterBaseEntity(list);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        T t = this.getColumnList(list);
        t.setPage(isPage);
        return t;
    }

    /**
     *
     * 过滤基类
     *
     * @param list
     * @return
     */
    private List<DataBaseField> filterBaseEntity(List<DataBaseField> list) {
        if (StringUtils.isNullOrEmpty(getFilterClass())) {
            return list;
        }
        List<String> fields = FieldUtils.getFieldString(getFilterClass());
        if (CollectionUtils.isEmpty(fields)){
            return list;
        }

        return list.stream().filter(item->!fields.contains(VelocityUtils.underline2Camel(item.getColumnName())))
                .collect(Collectors.toList());
    }

    /**
     * 获取模板中的属性名
     * @return
     */
    public abstract T getColumnList(List<DataBaseField> list);

    /**
     * 设置需要导入的 import
     * @param item
     * @return
     */
    protected String parseForImport(DataBaseField item) {
        String dataType = item.getDataType();
        if (VelocityUtils.JAVA_TYPE_IMPORT.containsKey(dataType)) {
            return VelocityUtils.JAVA_TYPE_IMPORT.get(dataType);
        }
        return "";
    }

    /**
     * 获取java的属性类型
     * @param field
     * @return
     */
    protected String getJavaType(DataBaseField field) {
        String dataType = field.getDataType();
        String columnType = field.getColumnType();
        if(VelocityUtils.COLUMN_TYPE_REFLECT_DATA_TYPE.containsKey(dataType)){
            if ("tinyint".equalsIgnoreCase(dataType)) {
                return columnType.indexOf("(1)") > 0 ? "Boolean" : "Integer";
            }else {
                return VelocityUtils.COLUMN_TYPE_REFLECT_DATA_TYPE.get(dataType);
            }
        }else {
            return "String";
        }
    }
}
